import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {//交互数据
    name:'张张小仙女',
    age:18,
    goodsListAll:[],
  },
  getters: {
    //筛选出所有num>0的数据
    newShopData(state){
      let newArr=[]//定义一个新的数组
      let list:any=state.goodsListAll
      // console.log(list);
      for(let obj of list){
        // console.log(obj);      
        for(let foods of obj.foods){
            // console.log(foods);
            if(foods.num>0){
              newArr.push(foods)
            }
        }
      }
      console.log(newArr);  //这里是num大于0的商品,每点击一个才会加一个对象,只要num>0才加不大于0就不加
      return newArr
    }
  },
  mutations: {//改变state的唯一方式
    changeName(state,val){
      state.name=val
    },
    //初始化详情数组
    changeListAll(state,val){
      for(let obj of val){//遍历的必须要是我们发送请求后,传过来的值
        // console.log(obj);
        for(let foods of obj.foods){
          // console.log(foods);
          foods.num=0
        }
      }
      state.goodsListAll=val
      // console.log(val);
    },
    //改变对象的数量
    changeNum(state,itemObj){
      let list:any=state.goodsListAll
      for(let obj of list){
        for(let foods of obj.foods){
          // console.log(foods.id);
          if(foods.id==itemObj.id){
            foods.num+=itemObj.num
          }
        }
      }
    }
  },
  actions: {
  },
  modules: {
  }
})
